<template>
  <ele-page>
    <!-- 搜索表单 -->
    <info-search @search="reload"/>
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        :highlight-current-row="true"
        :export-config="{ fileName: '就业统计-学生基础信息' }"
        cache-key="statisticInfoTable"
        :loadOnCreated="false"
        :loading="loading"
      >
        <template #toolbar>
          <el-button
            type="primary"
            class="ele-btn-icon"
            :icon="PlusOutlined"
            @click="openEdit()"
          >
            新增
          </el-button>
          <el-button
            type="danger"
            class="ele-btn-icon"
            :icon="DeleteOutlined"
            @click="removeBatch"
            :disabled="selections.length <=0"
          >
            删除
          </el-button>
          <el-button
            type="success"
            :icon="ConnectionOutlined"
            @click="openSetLead"
            :disabled="selections.length <=0">
            设置带兵
          </el-button>
          <el-button
            type="warning"
            :icon="ConnectionOutlined"
            @click="openSetDepartmentMajor"
            :disabled="selections.length <=0">
            设置专业
          </el-button>
          <el-button
            class="ele-btn-icon"
            :icon="UploadOutlined"
            @click="openImport"
          >
            导入
          </el-button>
          <el-button
            class="ele-btn-icon"
            :icon="DownloadOutlined"
            @click="exportData"
          >
            导出
          </el-button>
        </template>
        <template #action="{ row }">
          <el-link
            type="primary"
            underline="never"
            @click="openEdit(row)"
          >
            修改
          </el-link>
          <el-divider
            direction="vertical"
          />
          <el-link
            type="danger"
            underline="never"
            @click="removeBatch(row)"
          >
            删除
          </el-link>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <info-edit v-model="showEdit" :data="current" @done="reload"/>

    <set-lead v-model="showSetLead" :data="selections" @done="reload"></set-lead>

    <set-department-major v-model="showSetDepartmentMajor" :data="selections" @done="reload"></set-department-major>

    <import v-model="showImport" @done="reload"></import>
  </ele-page>
</template>

<script setup>
import {ref} from 'vue';
import {
  PlusOutlined,
  DeleteOutlined,
  DownloadOutlined,//下载
  MenuUnfoldOutlined,
  UploadOutlined,//上传
  ConnectionOutlined
} from '@/components/icons';
import {ElMessageBox} from 'element-plus';
import {EleMessage} from 'ele-admin-plus';
import InfoSearch from './components/student-info-search.vue';
import InfoEdit from './components/student-info-edit.vue';
import SetLead from "./components/student-set-lead.vue";
import SetDepartmentMajor from "./components/student-set-department-major.vue";
import Import from "./components/student-import.vue";
import {pageInfos, removeInfos, exportInfos, initGrade} from '@/api/manage/statistic/admin/studentInfo';

defineOptions({name: 'StatisticInfo'});

/** 表格实例 */
const tableRef = ref(null);

/** 表格列配置 */
const columns = ref([
  {
    type: 'selection',
    columnKey: 'selection',
    width: 50,
    align: 'center',
    fixed: 'left'
  },
  {
    prop: 'code',
    label: '学号',
    align: 'center',
    minWidth: 90
  },
  {
    prop: 'name',
    label: '姓名',
    align: 'center',
    minWidth: 90
  },
  {
    prop: 'campus',
    label: '校区',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'grade',
    label: '年级',
    align: 'center',
    minWidth: 90
  },
  {
    prop: 'department',
    label: '系别',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'major',
    label: '专业',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'studyState',
    label: '学籍状态',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'counsellorName',
    label: '辅导员',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'graduationYear',
    label: '毕业年份',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'organization',
    label: '组织',
    align: 'center',
    minWidth: 110
  },
  {
    columnKey: 'action',
    label: '操作',
    width: 160,
    align: 'center',
    slot: 'action',
    hideInPrint: true,
    hideInExport: true
  }
]);

/** 表格选中数据 */
const selections = ref([]);

/** 当前编辑数据 */
const current = ref(null);

/** 是否显示编辑弹窗 */
const showEdit = ref(false);

/** 提交状态 */
const loading = ref(false);

/** 表格数据源 */
const datasource = ({pages, where}) => {
  return pageInfos({...where, ...pages});
};

/** 搜索 */
const reload = (where) => {
  tableRef.value?.reload?.({page: 1, where});
};

/** 打开编辑弹窗 */
const openEdit = (row) => {
  current.value = row ?? null;
  showEdit.value = true;
};

/** 批量删除 */
const removeBatch = (row) => {
  const rows = row == null ? selections.value : [row];
  if (!rows.length) {
    EleMessage.error({message: '请至少选择一条数据', plain: true});
    return;
  }
  ElMessageBox.confirm(
    `是否确认删除姓名为"${rows.map((d) => d.name).join()}"的数据项?`,
    '系统提示',
    {type: 'warning', draggable: true}
  )
    .then(() => {
      const loading = EleMessage.loading({
        message: '请求中..',
        plain: true
      });
      removeInfos(rows.map((d) => d.id))
        .then(() => {
          loading.close();
          EleMessage.success({message: '删除成功', plain: true});
          reload();
        })
        .catch((e) => {
          loading.close();
          EleMessage.error({message: e.message, plain: true});
        });
    })
    .catch(() => {
    });
};


/** 导出数据 */
const exportData = () => {
  const loading = EleMessage.loading({
    message: '请求中..',
    plain: true
  });
  tableRef.value?.fetch?.(({where}) => {
    exportInfos(where)
      .then(() => {
        loading.close();
      })
      .catch((e) => {
        loading.close();
        EleMessage.error({message: e.message, plain: true});
      });
  });
};


// const loading = ref(false);

// const handleInitGrade = () => {
//   const loading = EleMessage.loading({
//     message: '请求中..',
//     plain: true
//   });
//   initGrade().then(res => {
//     loading.close();
//     EleMessage.success({message: '年级、系别、专业、毕业年份初始化成功', plain: true});
//   }).catch((e) => {
//     loading.close();
//     EleMessage.error({message: e.message, plain: true});
//   });
// }

const showSetLead = ref(false);
const openSetLead = () => {
  showSetLead.value = true;
}
const showSetDepartmentMajor = ref(false);
const openSetDepartmentMajor = () => {
  showSetDepartmentMajor.value = true;
}

const showImport = ref(false);
const openImport = () => {
  showImport.value = true;
}


</script>
